﻿Imports System.Globalization

Partial Public Class SetProduct_Shore
    Inherits System.Web.UI.Page

    Dim clsGet As New ClassFunctionGetData
    Dim clsFc As New ClassFunction
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not (IsPostBack) Then
            Dim lbPage As Label = CType(Master.FindControl("lbPage"), Label)
            lbPage.Text = "SET PRODUCT SHORE "
            LoadBOoking()
            ViewState("Load") = "Not"
            Timer1_Tick(sender, e)
        End If
    End Sub

    Private Sub LoadBOoking()
        PanelBookingItem.Visible = True
        PanelShoreProduct.Visible = False
        Dim ds As DataSet
        Dim DateFrom As String = ""
        Dim DateTo As String = ""
        If (txtDateSearch.Text.Trim <> "") And (txtDateToSearch.Text.Trim() <> "") Then
            DateFrom = txtDateSearch.Text.Split(" ")(2) & CDate(txtDateSearch.Text).ToString("MMdd")
            DateTo = txtDateToSearch.Text.Split(" ")(2) & CDate(txtDateToSearch.Text).ToString("MMdd")
        End If
        If (ddlStatusVPS.SelectedValue = "C") Then
            ds = clsGet.Get_Booking("", "", "", "'CV','CC','CM','CR','CA'", True, "L", txtShipVPS.Text, "", "", txtBookingVPS.Text, ddlDestinationSearch.SelectedValue, DateFrom, DateTo, txtProductSearch.Text)
        Else
            ds = clsGet.Get_Booking("", "", "", "'MO','OA','FN'", True, "L", txtShipVPS.Text, "", "'CV','CC','CM','CR','CA'", txtBookingVPS.Text, ddlDestinationSearch.SelectedValue, DateFrom, DateTo, txtProductSearch.Text)
        End If
        If (ddlProductLD.SelectedValue <> "A") Then
            Dim dsTemp As DataSet = ds.Copy
            Dim count As Integer = 0
            For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                Dim dt As DataTable = clsGet.Get_Booking_Product(ds.Tables(0).Rows(i).Item("Booking_no"))
                If (dt.Rows.Count > 0) Then
                    If (dt.Rows(0).Item("Product_Event") & "" <> ddlProductLD.SelectedValue) Then
                        dsTemp.Tables(0).Rows(i - count).Delete()
                        dsTemp.AcceptChanges()
                        count += 1
                    End If
                Else
                    dsTemp.Tables(0).Rows(i - count).Delete()
                    dsTemp.AcceptChanges()
                    count += 1
                End If
            Next
            ds = dsTemp
        ElseIf (ddlStatusVPS.SelectedValue = "W") Then
            Dim dsTemp As DataSet = ds.Copy
            Dim count As Integer = 0
            For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                Dim dt As DataTable = clsGet.Get_Booking_Product(ds.Tables(0).Rows(i).Item("Booking_no"))
                If (dt.Rows.Count > 0) Then
                    If (dt.Rows(0).Item("Product_Shore") & "" <> "") Then
                        dsTemp.Tables(0).Rows(i - count).Delete()
                        dsTemp.AcceptChanges()
                        count += 1
                    End If
                Else
                    dsTemp.Tables(0).Rows(i - count).Delete()
                    dsTemp.AcceptChanges()
                    count += 1
                End If
            Next
            ds = dsTemp
        ElseIf (ddlStatusVPS.SelectedValue = "S") Then
            Dim dsTemp As DataSet = ds.Copy
            Dim count As Integer = 0
            For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                Dim dt As DataTable = clsGet.Get_Booking_Product(ds.Tables(0).Rows(i).Item("Booking_no"))
                If (dt.Rows.Count > 0) Then
                    If (dt.Rows(0).Item("Product_Shore") & "" = "") Then
                        dsTemp.Tables(0).Rows(i - count).Delete()
                        dsTemp.AcceptChanges()
                        count += 1
                    End If
                Else
                    dsTemp.Tables(0).Rows(i - count).Delete()
                    dsTemp.AcceptChanges()
                    count += 1
                End If
            Next
            ds = dsTemp
        End If
        ds.AcceptChanges()
        If (ds.Tables(0).Rows.Count > 0) Then
            ViewState("ShoreProduct") = ds
            gridItem.DataSource = ViewState("ShoreProduct")
            gridItem.DataBind()
            lbAlert.Text = ""
        Else
            gridItem.DataSource = Nothing
            gridItem.DataBind()
            lbAlert.Text = "ไม่พบรายการ Booking"
        End If
    End Sub

    Public Sub Paginate(ByVal sender As Object, ByVal e As CommandEventArgs)
        Dim CurrentIndex As Integer = gridItem.PageIndex
        Select Case e.CommandArgument.ToString().ToLower()
            Case "first"
                gridItem.PageIndex = 0
            Case "prev"
                If (gridItem.PageIndex <> 0) Then
                    gridItem.PageIndex = CurrentIndex - 1
                End If
            Case "next"
                If (gridItem.PageIndex <> gridItem.PageCount) Then
                    gridItem.PageIndex = CurrentIndex + 1
                End If
            Case "last"
                gridItem.PageIndex = gridItem.PageCount
        End Select
        gridItem.DataSource = ViewState("ShoreProduct")
        gridItem.DataBind()
        ViewState("Load") = "Not"
        Timer1_Tick(sender, e)
    End Sub

    Protected Sub ddlPage_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim Page As GridViewRow = gridItem.BottomPagerRow
        Dim ddlPage As DropDownList = CType(Page.Cells(0).FindControl("ddlPage"), DropDownList)
        gridItem.PageIndex = ddlPage.SelectedIndex
        gridItem.DataSource = ViewState("ShoreProduct")
        gridItem.DataBind()
        ViewState("Load") = "Not"
        Timer1_Tick(sender, e)
    End Sub

    Private Sub gridItem_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gridItem.RowCommand
        Dim Index As Integer = Convert.ToInt32(e.CommandArgument)
        If (gridItem.PageIndex <> 0) Then
            Index = Index Mod (gridItem.PageIndex * 10)
        End If
        If e.CommandName = "Edit" Then
            PanelBookingItem.Visible = False
            PanelShoreProduct.Visible = True
            Dim dt As DataTable = clsGet.Get_Booking_Product(gridItem.DataKeys(Index).Values("Booking_no"))
            ViewState("Booking_no") = gridItem.DataKeys(Index).Values("Booking_no")

            Dim dsBooking As DataSet = clsGet.Get_BookingOneRecord(gridItem.DataKeys(Index).Values("Booking_no"))
            lbVehicle.Text = gridItem.DataKeys(Index).Values("Vehicle_Text")
            lbLOA.Text = dsBooking.Tables(0).Rows(0).Item("Vehicle_LOA")
            lbDraft.Text = dsBooking.Tables(0).Rows(0).Item("Vehicle_Draft")
            lbDWT.Text = dsBooking.Tables(0).Rows(0).Item("Vehicle_DWT")
            lbGRT.Text = dsBooking.Tables(0).Rows(0).Item("Vehicle_GRT")
            lbNation.Text = dsBooking.Tables(0).Rows(0).Item("Vehicle_CNTRY")
            lbDate.Text = dsBooking.Tables(0).Rows(0).Item("Date_From") & " ถึง " & dsBooking.Tables(0).Rows(0).Item("Date_To")
            lbBookingDateTime.Text = dsBooking.Tables(0).Rows(0).Item("Booking_Date") & " : " & dsBooking.Tables(0).Rows(0).Item("Booking_Time")
            lbLayTime.Text = dsBooking.Tables(0).Rows(0).Item("Lay_Time") & " hours"
            gridProduct.DataSource = dt
            gridProduct.DataBind()
            For i As Integer = 0 To gridProduct.Rows.Count - 1
                Dim ddlIndex As DropDownList = CType(gridProduct.Rows(i).FindControl("ddlIndex"), DropDownList)
                For j As Integer = 1 To gridProduct.Rows.Count
                    Dim Item As Integer = ddlIndex.Items.Count
                    ddlIndex.Items.Insert(Item, Item)
                    ddlIndex.Items(Item).Value = Item
                Next
                ddlIndex.SelectedValue = (i + 1).ToString()
            Next
            For i As Integer = 0 To gridProduct.Rows.Count - 1
                Dim ddlIndex As DropDownList = CType(gridProduct.Rows(i).FindControl("ddlIndex"), DropDownList)
                If Not (ddlIndex.Items.FindByValue(gridProduct.DataKeys(i).Values("Product_Index") & "") Is Nothing) Then
                    ddlIndex.ClearSelection()
                    ddlIndex.SelectedValue = gridProduct.DataKeys(i).Values("Product_Index") & ""
                End If
            Next
            If (gridItem.DataKeys(Index).Values("Status") & "" <> "MO") Then
                btnSave.Visible = False
            End If

        ElseIf e.CommandName = "Detail" Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " document.getElementById('ctl00_contentplaceholder1_IframeDetailPopup').src = '../FormPopup/Detail_VesselBerth_Popup.aspx';", True)
            Session("Booking_no_Detail") = gridItem.DataKeys(Index).Values("Booking_no")
            ModalPopupExtender5.Show()
        End If
    End Sub

    Private Sub gridItem_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridItem.RowDataBound
        If (e.Row.RowType = DataControlRowType.DataRow) Then
            Dim imgVPS As Image = CType(e.Row.FindControl("imgVPS"), Image)
            Dim imgMRI As Image = CType(e.Row.FindControl("imgMRI"), Image)
            Dim imgVPSComment As Image = CType(e.Row.FindControl("imgVPSComment"), Image)
            Dim imgMRIComment As Image = CType(e.Row.FindControl("imgMRIComment"), Image)
            Dim imgMKT As Image = CType(e.Row.FindControl("imgMKT"), Image)
            Dim imgProduct As Image = CType(e.Row.FindControl("imgProduct"), Image)
            Dim imgCustom As Image = CType(e.Row.FindControl("imgCustom"), Image)
            Dim imgFinance As Image = CType(e.Row.FindControl("imgFinance"), Image)
            clsFc.GetImageStatus(imgVPS, imgMRI, imgMKT, gridItem.DataKeys(e.Row.RowIndex).Values("VPS_approve"), gridItem.DataKeys(e.Row.RowIndex).Values("MRI_approve"), gridItem.DataKeys(e.Row.RowIndex).Values("Marketing_approve"), gridItem.DataKeys(e.Row.RowIndex).Values("Status"))
            clsFc.GetImageStatusMKT(imgProduct, imgCustom, imgFinance, gridItem.DataKeys(e.Row.RowIndex).Values("MKT_Product"), gridItem.DataKeys(e.Row.RowIndex).Values("MKT_Custom"), gridItem.DataKeys(e.Row.RowIndex).Values("MKT_Finance"))
            If (gridItem.DataKeys(e.Row.RowIndex).Values("Remark_VPS") & "" <> "") Then
                imgVPSComment.Visible = True
            Else
                imgVPSComment.Visible = False
            End If
            If (gridItem.DataKeys(e.Row.RowIndex).Values("Remark_MRI") & "" <> "") Then
                imgMRIComment.Visible = True
            Else
                imgMRIComment.Visible = False
            End If

            Dim imgConfrim As Image = CType(e.Row.FindControl("imgConfrim"), Image)
            If (gridItem.DataKeys(e.Row.RowIndex).Values("Confirm_Time") & "" = "Y") Then
                imgConfrim.ImageUrl = "~/Image/4558.png"
            Else
                imgConfrim.ImageUrl = "~/Image/4557.gif"
            End If

            Dim Status As String = ""
            Dim Tooltip As String = ""
            clsGet.GetStatus_Working(gridItem.DataKeys(e.Row.RowIndex).Values("Booking_no"), gridItem.DataKeys(e.Row.RowIndex).Values("PORT"), Status, Tooltip)
            e.Row.Cells(1).Text = Status
            e.Row.Cells(1).ToolTip = Tooltip

            If (gridItem.DataKeys(e.Row.RowIndex).Values("Date_From") & "" <> "") Then
                If (gridItem.DataKeys(e.Row.RowIndex).Values("Date_From") & "" = gridItem.DataKeys(e.Row.RowIndex).Values("Date_To") & "") Then
                    e.Row.Cells(2).Text = gridItem.DataKeys(e.Row.RowIndex).Values("Date_To")
                Else
                    e.Row.Cells(2).Text = gridItem.DataKeys(e.Row.RowIndex).Values("Date_From").ToString.Substring(0, 2) & "-" & gridItem.DataKeys(e.Row.RowIndex).Values("Date_To")
                End If

                e.Row.Cells(3).Text = gridItem.DataKeys(e.Row.RowIndex).Values("Booking_Date") & " : " & gridItem.DataKeys(e.Row.RowIndex).Values("Booking_Time")
                e.Row.Cells(4).Text = e.Row.Cells(4).Text & " hours"
                If (gridItem.DataKeys(e.Row.RowIndex).Values("Vehicle_Destination") & "" = "O") Then
                    If (gridItem.DataKeys(e.Row.RowIndex).Values("Vehicle_TypeBuy") & "" = "F") Then
                        e.Row.Cells(17).Text = "FOB" 'รูปแบบการซื้อขาย
                    ElseIf (gridItem.DataKeys(e.Row.RowIndex).Values("Vehicle_TypeBuy") & "" = "C") Then
                        e.Row.Cells(17).Text = "CFR"
                    ElseIf (gridItem.DataKeys(e.Row.RowIndex).Values("Vehicle_TypeBuy") & "" = "I") Then
                        e.Row.Cells(17).Text = "CIF"
                    Else
                        e.Row.Cells(17).Text = "อื่นๆ" 'รูปแบบการซื้อขาย
                    End If
                Else
                    e.Row.Cells(17).Text = "" 'รูปแบบการซื้อขาย
                    e.Row.Cells(16).Text = ""
                End If
                If (gridItem.DataKeys(e.Row.RowIndex).Values("Demurrage_Money") & "" = "B") Then
                    If (gridItem.DataKeys(e.Row.RowIndex).Values("Demurrage") & "" = "") Then
                        e.Row.Cells(16).Text = "- BAHT"
                    Else
                        e.Row.Cells(16).Text = gridItem.DataKeys(e.Row.RowIndex).Values("Demurrage") & " BAHT "
                    End If
                ElseIf (gridItem.DataKeys(e.Row.RowIndex).Values("Demurrage_Money") & "" = "U") Then
                    If (gridItem.DataKeys(e.Row.RowIndex).Values("Demurrage") & "" = "") Then
                        e.Row.Cells(16).Text = "- USD"
                    Else
                        e.Row.Cells(16).Text = gridItem.DataKeys(e.Row.RowIndex).Values("Demurrage") & " USD "
                    End If
                End If
            End If

            Dim Product_Name As String = ""
            Dim Product_Amount As String = ""
            Dim Owner_name As String = ""
            Dim dtLog As DataTable
            dtLog = clsGet.Get_Booking_Product(gridItem.DataKeys(e.Row.RowIndex).Values("Booking_no"))
            For Each dr As DataRow In dtLog.Rows
                If (dr.Item("Product_name") & "" <> "") Then
                    If (Product_Name <> "") Then
                        Product_Name &= "<br />" & dr.Item("Product_name") & ""
                        Product_Amount &= "<br />" & dr.Item("Product_Amount") & ""
                        Owner_name &= "<br />" & dr.Item("Owner_Product_Name") & ""
                    Else
                        Product_Name = dr.Item("Product_name") & ""
                        Product_Amount = dr.Item("Product_Amount") & ""
                        Owner_name = dr.Item("Owner_Product_Name") & ""
                    End If
                End If
            Next
            If (gridItem.DataKeys(e.Row.RowIndex).Values("BunkerFO_Point") & "" <> "") And (gridItem.DataKeys(e.Row.RowIndex).Values("BunkerFO_Point") & "" <> "0") Then
                'BunkerFO 1 คัน,BunkerDO 1 คัน
                If Product_Name = "" Then
                    Product_Name = "BunkerFO"
                    Product_Amount = gridItem.DataKeys(e.Row.RowIndex).Values("BunkerFO_Point") & " คัน"
                Else
                    Product_Name &= "<br />" & "BunkerFO"
                    Product_Amount &= "<br />" & gridItem.DataKeys(e.Row.RowIndex).Values("BunkerFO_Point") & " คัน"
                End If
            End If
            If (gridItem.DataKeys(e.Row.RowIndex).Values("BunkerDO_Point") & "" <> "") And (gridItem.DataKeys(e.Row.RowIndex).Values("BunkerDO_Point") & "" <> "0") Then
                If Product_Name = "" Then
                    Product_Name = "BunkerDO"
                    Product_Amount = gridItem.DataKeys(e.Row.RowIndex).Values("BunkerDO_Point") & " คัน"
                Else
                    Product_Name &= "<br />" & "BunkerDO"
                    Product_Amount &= "<br />" & gridItem.DataKeys(e.Row.RowIndex).Values("BunkerDO_Point") & " คัน"
                End If
            End If
            Dim File_Upload As String = ""
            If (gridItem.DataKeys(e.Row.RowIndex).Values("File_Upload") & "" <> "") Then
                For i As Integer = 0 To gridItem.DataKeys(e.Row.RowIndex).Values("File_Upload").ToString.Split(",").Length - 1
                    If (gridItem.DataKeys(e.Row.RowIndex).Values("File_Upload").ToString.Split(",")(i) & "" <> "") Then
                        If File_Upload = "" Then
                            File_Upload = gridItem.DataKeys(e.Row.RowIndex).Values("File_Upload").ToString.Split(",")(i)
                        Else
                            File_Upload &= "<br />" & gridItem.DataKeys(e.Row.RowIndex).Values("File_Upload").ToString.Split(",")(i)
                        End If
                    End If
                Next
            End If
            e.Row.Cells(6).Text = File_Upload
            e.Row.Cells(7).Text = Product_Name
            e.Row.Cells(8).Text = Product_Amount
            Dim Unit() As String = (gridItem.DataKeys(e.Row.RowIndex).Values("Product_Unit") & "").ToString.Split(",")
            Dim Text As String = ""
            For i As Integer = 0 To Unit.Length - 1
                If (Unit(i) & "" <> "") Then
                    If (Text = "") Then
                        Text = Unit(i)
                    Else
                        Text &= "<br />" & Unit(i)
                    End If
                End If
            Next
            e.Row.Cells(9).Text = Text
            Text = ""
            Unit = (gridItem.DataKeys(e.Row.RowIndex).Values("BUYER") & "").ToString.Split(",")
            For i As Integer = 0 To Unit.Length - 1
                If (Unit(i) & "" <> "") Then
                    If (Text = "") Then
                        Text = Unit(i)
                    Else
                        Text &= "<br />" & Unit(i)
                    End If
                End If
            Next
            e.Row.Cells(10).Text = Text
            Text = ""
            Unit = (gridItem.DataKeys(e.Row.RowIndex).Values("SELLER") & "").ToString.Split(",")
            For i As Integer = 0 To Unit.Length - 1
                If (Unit(i) & "" <> "") Then
                    If (Text = "") Then
                        Text = Unit(i)
                    Else
                        Text &= "<br />" & Unit(i)
                    End If
                End If
            Next
            e.Row.Cells(11).Text = Text
            If (clsFc.GetStatusCancel(gridItem.DataKeys(e.Row.RowIndex).Values("Status"))) Then
                e.Row.BackColor = Drawing.Color.Red
            Else
                Dim Product_Event As String = clsGet.Get_ProductEvent(gridItem.DataKeys(e.Row.RowIndex).Values("Booking_no"))
                If (Product_Event = "L") Then
                    If (gridItem.DataKeys(e.Row.RowIndex).Values("Vehicle_Destination") & "" = "O") Then
                        e.Row.BackColor = Drawing.Color.FromName("#6698FF")
                    Else
                        e.Row.BackColor = Drawing.Color.FromName("#A0CFEC")
                    End If
                    If (gridItem.DataKeys(e.Row.RowIndex).Values("PORT") & "" = "B") Then
                        e.Row.Cells(11).Text = Owner_name
                    End If
                ElseIf (Product_Event = "D") Then
                    If (gridItem.DataKeys(e.Row.RowIndex).Values("Vehicle_Destination") & "" = "O") Then
                        e.Row.BackColor = Drawing.Color.FromName("#347C17")
                    Else
                        e.Row.BackColor = Drawing.Color.FromName("#52D017")
                    End If
                    If (gridItem.DataKeys(e.Row.RowIndex).Values("PORT") & "" = "B") Then
                        e.Row.Cells(10).Text = Owner_name
                    End If
                Else
                    e.Row.BackColor = Drawing.Color.White
                End If
            End If
        ElseIf (e.Row.RowType = DataControlRowType.Pager) Then
            Dim Page As GridViewRow = gridItem.BottomPagerRow
            Dim ddlPage As DropDownList = CType(e.Row.FindControl("ddlPage"), DropDownList)
            Dim lblPageCount As Label = CType(e.Row.FindControl("lbPageCount"), Label)
            If Not (ddlPage Is Nothing) Then
                For i As Integer = 0 To gridItem.PageCount - 1
                    Dim intPageNumber As Integer = i + 1
                    Dim IsItem As New ListItem(intPageNumber.ToString())
                    If (i = gridItem.PageIndex) Then
                        IsItem.Selected = True
                    End If
                    ddlPage.Items.Add(IsItem)
                Next
            End If
            If Not (lblPageCount Is Nothing) Then
                lblPageCount.Text = gridItem.PageCount.ToString()
            End If
        End If
    End Sub
    Protected Sub ddlShore_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim ddlShore = DirectCast(sender, DropDownList)
        Dim ClientID As String = ddlShore.ClientID
        For i As Integer = 0 To gridProduct.Rows.Count - 1
            Dim ddlLine As DropDownList = CType(gridProduct.Rows(i).FindControl("ddlLine"), DropDownList)
            If Not (ddlLine Is Nothing) Then
                If (ddlLine.ClientID.Replace("ddlLine", "") = ClientID.Replace("ddlShore", "")) Then
                    'ทำการ Bind ค่า Line
                    Dim ds As DataTable = clsGet.Get_LineForProduct(ddlShore.SelectedValue, gridProduct.DataKeys(i).Values("Group_no") & "")
                    If (ds.Rows.Count > 0) Then
                        With ddlLine
                            .Items.Clear()
                            .ClearSelection()
                            .DataTextField = "Line_Name"
                            .DataValueField = "Line_Code"
                            .DataSource = ds
                            .DataBind()
                            .Items.Insert(0, "<--กรุณาเลือก-->")
                            .Items(0).Value = "0"
                        End With
                    Else
                        With ddlLine
                            .Items.Clear()
                            .Items.Insert(0, "<--ไม่พบรายการ Line-->")
                            .Items(0).Value = "0"
                        End With
                    End If
                End If
            End If
        Next
    End Sub

    Protected Sub ddlLine_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim ddlLine = DirectCast(sender, DropDownList)
        Dim ClientID As String = ddlLine.ClientID
        For i As Integer = 0 To gridProduct.Rows.Count - 1
            Dim ddlShore As DropDownList = CType(gridProduct.Rows(i).FindControl("ddlShore"), DropDownList)
            If Not (ddlShore Is Nothing) Then
                If (ddlShore.ClientID.Replace("ddlShore", "") = ClientID.Replace("ddlLine", "")) Then
                    'ทำการ Bind ค่า Line
                    Dim ddlFacility As DropDownList = CType(gridProduct.Rows(i).FindControl("ddlFacility"), DropDownList)
                    Dim ds As DataTable = clsGet.Get_FacilityForProduct(ddlShore.SelectedValue, ddlLine.SelectedValue, gridProduct.DataKeys(i).Values("Group_no") & "")
                    If (ds.Rows.Count > 0) Then
                        With ddlFacility
                            .Items.Clear()
                            .ClearSelection()
                            .DataTextField = "Facility_Name"
                            .DataValueField = "Facility_Code"
                            .DataSource = ds
                            .DataBind()
                            .Items.Insert(0, "<--กรุณาเลือก-->")
                            .Items(0).Value = "0"
                        End With
                    Else
                        With ddlFacility
                            .Items.Clear()
                            .Items.Insert(0, "<--ไม่พบรายการ Facility-->")
                            .Items(0).Value = "0"
                        End With
                    End If
                End If
            End If
        Next
    End Sub

    Private Sub gridItem_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gridItem.RowEditing

    End Sub

    Private Sub gridProduct_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridProduct.RowDataBound
        If (e.Row.RowType = DataControlRowType.DataRow) Then
            Dim ddlShore As DropDownList = CType(e.Row.FindControl("ddlShore"), DropDownList)
            Dim dt As DataTable = clsGet.Get_BookingWharf(ViewState("Booking_no"))
            If (dt.Rows.Count = 0) Then
                LoadBOoking()
                ViewState("Load") = "Not"
                Timer1_Tick(sender, e)
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('ไม่พบข้อมูลการจองคิวท่าเรือ');", True)
                Exit Sub
            End If
            Dim BookingWharf As String = dt.Rows(0).Item("BookingWharf") & ""
            lbWharf.Text = dt.Rows(0).Item("Wharf") & ""
            Dim ds As DataTable = clsGet.Get_ShoreForProduct(gridProduct.DataKeys(e.Row.RowIndex).Values("Group_no") & "", BookingWharf, gridProduct.DataKeys(e.Row.RowIndex).Values("Product_Event") & "")
            If (ds.Rows.Count > 0) Then
                With ddlShore
                    .ClearSelection()
                    .Items.Clear()
                    .DataTextField = "Shore_Name"
                    .DataValueField = "Shore_Code"
                    .DataSource = ds
                    .DataBind()
                    .Items.Insert(0, "<--กรุณาเลือก-->")
                    .Items(0).Value = "0"
                End With
                If Not (ddlShore.Items.FindByValue(gridProduct.DataKeys(e.Row.RowIndex).Values("Product_Shore") & "") Is Nothing) Then
                    ddlShore.SelectedValue = gridProduct.DataKeys(e.Row.RowIndex).Values("Product_Shore") & ""

                    Dim ddlLine As DropDownList = CType(e.Row.FindControl("ddlLine"), DropDownList)
                    ds = clsGet.Get_LineForProduct(ddlShore.SelectedValue, gridProduct.DataKeys(e.Row.RowIndex).Values("Group_no") & "")
                    If (ds.Rows.Count > 0) Then
                        With ddlLine
                            .Items.Clear()
                            .ClearSelection()
                            .DataTextField = "Line_Name"
                            .DataValueField = "Line_Code"
                            .DataSource = ds
                            .DataBind()
                            .Items.Insert(0, "<--กรุณาเลือก-->")
                            .Items(0).Value = "0"
                        End With

                        If Not (ddlLine.Items.FindByValue(gridProduct.DataKeys(e.Row.RowIndex).Values("Product_Line") & "") Is Nothing) Then
                            ddlLine.SelectedValue = gridProduct.DataKeys(e.Row.RowIndex).Values("Product_Line") & ""

                            Dim ddlFacility As DropDownList = CType(e.Row.FindControl("ddlFacility"), DropDownList)
                            ds = clsGet.Get_FacilityForProduct(ddlShore.SelectedValue, ddlLine.SelectedValue, gridProduct.DataKeys(e.Row.RowIndex).Values("Group_no") & "")
                            If (ds.Rows.Count > 0) Then
                                With ddlFacility
                                    .Items.Clear()
                                    .ClearSelection()
                                    .DataTextField = "Facility_Name"
                                    .DataValueField = "Facility_Code"
                                    .DataSource = ds
                                    .DataBind()
                                    .Items.Insert(0, "<--กรุณาเลือก-->")
                                    .Items(0).Value = "0"
                                End With

                                If Not (ddlFacility.Items.FindByValue(gridProduct.DataKeys(e.Row.RowIndex).Values("Product_Facility") & "") Is Nothing) Then
                                    ddlFacility.SelectedValue = gridProduct.DataKeys(e.Row.RowIndex).Values("Product_Facility") & ""
                                End If
                            Else
                                With ddlFacility
                                    .Items.Clear()
                                    .Items.Insert(0, "<--ไม่พบรายการ Facility-->")
                                    .Items(0).Value = "0"
                                End With
                            End If

                        End If

                    Else
                        With ddlLine
                            .Items.Clear()
                            .Items.Insert(0, "<--ไม่พบรายการ Line-->")
                            .Items(0).Value = "0"
                        End With
                    End If

                End If
            Else
                With ddlShore
                    .ClearSelection()
                    .Items.Clear()
                    .Items.Insert(0, "<--ไม่พบข้อมูล Shore-->")
                    .Items(0).Value = "0"
                End With
            End If
            If (gridProduct.DataKeys(e.Row.RowIndex).Values("Product_Event") = "D") Then
                e.Row.Cells(0).Text = "Disch"
            ElseIf (gridProduct.DataKeys(e.Row.RowIndex).Values("Product_Event") = "L") Then
                e.Row.Cells(0).Text = "Load"
            End If
        End If
    End Sub

    Private Sub btnBack_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBack.Click
        LoadBOoking()
        ViewState("Load") = "Not"
        Timer1_Tick(sender, e)
    End Sub

    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        For i As Integer = 0 To gridProduct.Rows.Count - 1
            Dim ddlShore As DropDownList = CType(gridProduct.Rows(i).FindControl("ddlShore"), DropDownList)
            Dim ddlLine As DropDownList = CType(gridProduct.Rows(i).FindControl("ddlLine"), DropDownList)
            Dim ddlFacility As DropDownList = CType(gridProduct.Rows(i).FindControl("ddlFacility"), DropDownList)
            Dim txtFacility As TextBox = CType(gridProduct.Rows(i).FindControl("txtFacility"), TextBox)
            If (ddlShore.SelectedValue = "0") Then
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('กรุณาทำการระบุ Shore');", True)
                Exit Sub
            End If
            If (ddlLine.SelectedValue = "0") Then
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('กรุณาทำการระบุ Line');", True)
                Exit Sub
            End If
            If (ddlFacility.SelectedValue = "0") Then
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('กรุณาทำการระบุ Facility');", True)
                Exit Sub
            End If
        Next

        Dim conn As New System.Data.SqlClient.SqlConnection(connString)
        Dim clsDB As New ClassDatabase
        Dim clsFc As New ClassFunction
        Dim condition As Boolean = True
        Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
        Dim BP_Code As String = "''"
        Application.Lock()
        conn.Open()

        tran = clsDB.BeginTran(conn)
        clsFc.UpdateClear_Index(ViewState("Booking_no"), conn, tran)
        For i As Integer = 0 To gridProduct.Rows.Count - 1
            Dim ddlIndex As DropDownList = CType(gridProduct.Rows(i).FindControl("ddlIndex"), DropDownList)
            Dim ddlShore As DropDownList = CType(gridProduct.Rows(i).FindControl("ddlShore"), DropDownList)
            Dim ddlLine As DropDownList = CType(gridProduct.Rows(i).FindControl("ddlLine"), DropDownList)
            Dim ddlFacility As DropDownList = CType(gridProduct.Rows(i).FindControl("ddlFacility"), DropDownList)
            If (clsFc.UpdateBooking_Product_Index(ViewState("Booking_no"), gridProduct.DataKeys(i).Values("Product_no"), ddlIndex.SelectedValue, ddlShore.SelectedValue, ddlLine.SelectedValue, ddlFacility.SelectedValue, gridProduct.DataKeys(i).Values("Product_Buyer_name"), gridProduct.DataKeys(i).Values("Product_Seller_name"), BP_Code, gridProduct.DataKeys(i).Values("LoadDestination"), gridProduct.DataKeys(i).Values("DischDestination"), gridProduct.DataKeys(i).Values("Product_Amount"), "", conn, tran) < 1) Then
                condition = False
            Else
                BP_Code &= ",'" & ViewState("Booking_no") & gridProduct.DataKeys(i).Values("Product_no") & ddlIndex.SelectedValue & "'"
            End If
        Next

        If (condition) Then
            clsDB.CommitTran(tran)
            LoadBOoking()
            ViewState("Load") = "Not"
            Timer1_Tick(sender, e)
            ViewState("Booking_no") = ""
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('บันทึกข้อมูลเสร็จสมบูรณ์ !!');", True)
        Else
            clsDB.RollBackTran(tran)
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('เกิดความผิดพลาดในการบันทึก');", True)
        End If
        Application.UnLock()
        conn.Dispose()
    End Sub


    Private Sub gridItem_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridItem.RowCreated
        If e.Row.RowType = DataControlRowType.Header Then
            Dim Grid As GridView = CType(sender, GridView)
            e.Row.Cells.Clear()
            Dim GridRow As GridViewRow = New GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert)
            Dim GridRow1 As GridViewRow = New GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert)

            Dim table As TableCell = New TableCell()
            table.Text = "BOOKING NO."
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "STATUS"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "LAY CAN/LAY DAY"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "REQUIRE DATE/TIME"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "LAY TIME"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "VESSEL NAME"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "ATTACHMENT"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "COMMODITY"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "QUANTITY"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "UNIT"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "BUYER"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)


            table = New TableCell()
            table.Text = "SELLER"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "APPROVE BY MKT"
            table.HorizontalAlign = HorizontalAlign.Center
            table.ColumnSpan = 5
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "APPROVE BY"
            table.HorizontalAlign = HorizontalAlign.Center
            table.ColumnSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "TARIFF"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "AGENT"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "WHARF"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "PLAN BERTH DATE"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "PLAN BERTH TIME"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "PLAN DEPART DATE"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "PLAN DEPART TIME"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "CONFIRM PLAN BERTH"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "REMAIN LAYTIME(ESTIMATE)"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "REMARK"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = ""
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            Grid.Controls(0).Controls.AddAt(0, GridRow1)

            table = New TableCell()
            table.Text = "PRODUCT"
            table.HorizontalAlign = HorizontalAlign.Center
            table.Wrap = True
            GridRow.Cells.Add(table)

            table = New TableCell()
            table.Text = "CUSTOM"
            table.HorizontalAlign = HorizontalAlign.Center
            table.Wrap = True
            GridRow.Cells.Add(table)

            table = New TableCell()
            table.Text = "FINANCE"
            table.HorizontalAlign = HorizontalAlign.Center
            table.Wrap = True
            GridRow.Cells.Add(table)

            table = New TableCell()
            table.Text = "DEMURRAGE"
            table.HorizontalAlign = HorizontalAlign.Center
            table.Wrap = True
            GridRow.Cells.Add(table)

            table = New TableCell()
            table.Text = "TERM"
            table.HorizontalAlign = HorizontalAlign.Center
            table.Wrap = True
            GridRow.Cells.Add(table)

            table = New TableCell()
            table.Text = "PTMP"
            table.HorizontalAlign = HorizontalAlign.Center
            table.Wrap = True
            GridRow.Cells.Add(table)

            table = New TableCell()
            table.Text = "PTMI"
            table.HorizontalAlign = HorizontalAlign.Center
            table.Wrap = True
            GridRow.Cells.Add(table)

            Grid.Controls(0).Controls.AddAt(1, GridRow)
        End If
    End Sub

    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If (ViewState("Load") & "" <> "Not") Then
            LoadBOoking()
        Else
            ViewState("Load") = ""
        End If
        For i As Integer = 0 To gridItem.Rows.Count - 1
            Dim dsBooking As DataSet = clsGet.Get_BookingOneRecord(gridItem.DataKeys(i).Values("Booking_no"))
            If (dsBooking.Tables(0).Rows(0).Item("Lay_Time") & "" <> "0") Then
                Dim dtLogIn As DataTable = clsGet.Get_Booking_LogSheet_In(gridItem.DataKeys(i).Values("Booking_no"))
                If Not (dtLogIn.Rows.Count = 0) Then
                    If Not (dtLogIn.Rows(0).Item("Berthing_Date") & "" = "") Or (dtLogIn.Rows(0).Item("Berthing_TimeTo") & "" = "") Then
                        If Not (dtLogIn.Rows(0).Item("POB_Date") & "" = "") Or (dtLogIn.Rows(0).Item("POB_Time") & "" = "") Then
                            Dim dtProduct As DataTable = clsGet.Get_Booking_Product(gridItem.DataKeys(i).Values("Booking_no"))
                            If Not (dtProduct.Rows.Count = 0) Then
                                If (dsBooking.Tables(0).Rows(0).Item("PORT") & "" = "L") Then
                                    Dim Product_no As String = ""
                                    Try
                                        Dim sort As Integer = 0
                                        For Each dr As DataRow In dtProduct.Rows
                                            If (CInt(dr.Item("Product_Index") & "") > sort) Then
                                                sort = CInt(dr.Item("Product_Index"))
                                                Product_no = dr.Item("Product_no")
                                            End If
                                        Next
                                    Catch ex As Exception

                                    End Try
                                    Dim dtLoad As DataTable = clsGet.Get_Load_Unload_Vessel(gridItem.DataKeys(i).Values("Booking_no"))
                                    Dim NOR As String = ""
                                    Dim NOR_Time As String = ""
                                    Dim POB As String = ""
                                    Dim POB_Time As String = ""
                                    If (dtLoad.Rows.Count > 0) Then
                                        If (dtLoad.Rows(0).Item("NOR_Date") & "" <> "") Then
                                            NOR = dtLoad.Rows(0).Item("NOR_Date").ToString.Substring(0, 2) & " " & GetNameMonth(dtLoad.Rows(0).Item("NOR_Date").ToString.Substring(3, 2)) & " 20" & dtLoad.Rows(0).Item("NOR_Date").ToString.Substring(6, 2)
                                            NOR_Time = dtLoad.Rows(0).Item("NOR_Time") & ""
                                        Else
                                            NOR = dtLogIn.Rows(0).Item("NOR_Date") & ""
                                            NOR_Time = dtLogIn.Rows(0).Item("NOR_Time") & ""
                                        End If
                                    Else
                                        NOR = dtLogIn.Rows(0).Item("NOR_Date") & ""
                                        NOR_Time = dtLogIn.Rows(0).Item("NOR_Time") & ""
                                    End If
                                    If (dtLogIn.Rows(0).Item("POB_Date") & "" <> "") Then
                                        If (dtLogIn.Rows(0).Item("Up_Date") & "" <> "") Then
                                            If (CDate(dtLogIn.Rows(0).Item("POB_Date")) < CDate(dtLogIn.Rows(0).Item("Up_Date"))) Then
                                                POB = dtLogIn.Rows(0).Item("Up_Date") & ""
                                                POB_Time = dtLogIn.Rows(0).Item("Up_Time") & ""
                                            ElseIf (CDate(dtLogIn.Rows(0).Item("POB_Date")) = CDate(dtLogIn.Rows(0).Item("Up_Date"))) Then
                                                If (CInt(dtLogIn.Rows(0).Item("POB_Time").ToString().Split(":")(0)) < CInt(dtLogIn.Rows(0).Item("Up_Time").ToString().Split(":")(0))) Then
                                                    POB = dtLogIn.Rows(0).Item("Up_Date") & ""
                                                    POB_Time = dtLogIn.Rows(0).Item("Up_Time") & ""
                                                ElseIf (CInt(dtLogIn.Rows(0).Item("POB_Time").ToString().Split(":")(0)) = CInt(dtLogIn.Rows(0).Item("Up_Time").ToString().Split(":")(0))) Then
                                                    If (CInt(dtLogIn.Rows(0).Item("POB_Time").ToString().Split(":")(1)) < CInt(dtLogIn.Rows(0).Item("Up_Time").ToString().Split(":")(1))) Then
                                                        POB = dtLogIn.Rows(0).Item("Up_Date") & ""
                                                        POB_Time = dtLogIn.Rows(0).Item("Up_Time") & ""
                                                    Else
                                                        POB = dtLogIn.Rows(0).Item("POB_Date") & ""
                                                        POB_Time = dtLogIn.Rows(0).Item("POB_Time") & ""
                                                    End If
                                                Else
                                                    POB = dtLogIn.Rows(0).Item("POB_Date") & ""
                                                    POB_Time = dtLogIn.Rows(0).Item("POB_Time") & ""
                                                End If
                                            Else
                                                POB = dtLogIn.Rows(0).Item("POB_Date") & ""
                                                POB_Time = dtLogIn.Rows(0).Item("POB_Time") & ""
                                            End If
                                        Else
                                            POB = dtLogIn.Rows(0).Item("POB_Date") & ""
                                            POB_Time = dtLogIn.Rows(0).Item("POB_Time") & ""
                                        End If
                                    Else
                                        POB = dtLogIn.Rows(0).Item("Up_Date") & ""
                                        POB_Time = dtLogIn.Rows(0).Item("Up_Time") & ""
                                    End If

                                    dtLoad = clsGet.Get_Load_Unload_Product(gridItem.DataKeys(i).Values("Booking_no"), Product_no)
                                    Dim Dis_Date As String = ""
                                    Dim Dis_Time As String = ""
                                    For Each dl As DataRow In dtLoad.Rows
                                        Try
                                            If (Dis_Date = "") Then
                                                Dis_Date = dl.Item("LU_Disconnect_Date") & ""
                                                Dis_Time = dl.Item("LU_Disconnect_To") & ""
                                            End If
                                            Dim _DD As String = Dis_Date.Substring(0, 2) & " " & GetNameMonth(Dis_Date.Substring(3, 2)) & " " & CInt("20" & Dis_Date.Substring(6, 2))
                                            Dim _D2 As String = dl.Item("LU_Disconnect_Date").Substring(0, 2) & " " & GetNameMonth(dl.Item("LU_Disconnect_Date").Substring(3, 2)) & " " & CInt("20" & dl.Item("LU_Disconnect_Date").Substring(6, 2))
                                            If (CDate(_DD) < CDate(_D2)) Then
                                                Dis_Date = dl.Item("LU_Disconnect_Date") & ""
                                                Dis_Time = dl.Item("LU_Disconnect_To") & ""
                                            ElseIf (CDate(_DD) = CDate(_D2)) Then
                                                If (CInt(Dis_Time.Split(":")(0)) < CInt(dl.Item("LU_Disconnect_To").ToString.Split(":")(0))) Then
                                                    Dis_Date = dl.Item("LU_Disconnect_Date") & ""
                                                    Dis_Time = dl.Item("LU_Disconnect_To") & ""
                                                ElseIf (CInt(Dis_Time.Split(":")(0)) = CInt(dl.Item("LU_Disconnect_To").ToString.Split(":")(0))) Then
                                                    Try
                                                        If (CInt(Dis_Time.Split(":")(1)) < CInt(dl.Item("LU_Disconnect_To").ToString.Split(":")(1))) Then
                                                            Dis_Date = dl.Item("LU_Disconnect_Date") & ""
                                                            Dis_Time = dl.Item("LU_Disconnect_To") & ""
                                                        End If
                                                    Catch ex As Exception

                                                    End Try
                                                End If
                                            End If
                                        Catch ex As Exception

                                        End Try

                                    Next
                                    Dim Berthing_Date As String = dtLogIn.Rows(0).Item("Berthing_Date")
                                    Try
                                        Try
                                            If (CInt(dtLogIn.Rows(0).Item("Berthing_TimeTo").ToString().Split(":")(0)) < CInt(dtLogIn.Rows(0).Item("Berthing_TimeFrom").ToString.Split(":")(0))) Then
                                                Berthing_Date = CDate(Berthing_Date).AddDays(1).ToString("dd MMM", New CultureInfo("en-US")) & " " & Berthing_Date.Split(" ")(2)
                                            End If
                                        Catch ex As Exception

                                        End Try
                                    Catch ex As Exception

                                    End Try
                                    If (dtLoad.Rows.Count > 0) Then
                                        gridItem.Rows(i).Cells(28).Text = clsGet.ComputeRemainLaytime(Berthing_Date, dtLogIn.Rows(0).Item("Berthing_TimeTo"), dsBooking.Tables(0).Rows(0).Item("Lay_Time"), dsBooking.Tables(0).Rows(0).Item("Date_From"), dsBooking.Tables(0).Rows(0).Item("Date_To"), POB, POB_Time, Dis_Date & "", Dis_Time & "", NOR, NOR_Time)
                                    Else
                                        gridItem.Rows(i).Cells(28).Text = clsGet.ComputeRemainLaytime(Berthing_Date, dtLogIn.Rows(0).Item("Berthing_TimeTo"), dsBooking.Tables(0).Rows(0).Item("Lay_Time"), dsBooking.Tables(0).Rows(0).Item("Date_From"), dsBooking.Tables(0).Rows(0).Item("Date_To"), POB, POB_Time, "", "", NOR, NOR_Time)
                                    End If

                                Else
                                    Dim dtProductBulk As DataTable = clsGet.Get_Booking_Product(gridItem.DataKeys(i).Values("Booking_no"))
                                    If (dtProductBulk.Rows.Count > 0) Then
                                        Dim chkCoil As Boolean = False
                                        For Each dx As DataRow In dtProductBulk.Rows
                                            If (dx.Item("Group_no") & "" = "00007") Then
                                                chkCoil = True
                                                Exit For
                                            End If
                                        Next
                                        If (chkCoil) Then
                                           gridItem.Rows(i).Cells(28).Text = clsGet.ComputeRemainLaytimeBulk(dsBooking.Tables(0).Rows(0).Item("Lay_Time"), gridItem.DataKeys(i).Values("Booking_no"))
                                        End If
                                    End If
                                End If
                                If (gridItem.Rows(i).Cells(28).Text <> "") Then
                                    If (gridItem.Rows(i).Cells(28).Text.Substring(0, 1) = "-") Then
                                        gridItem.Rows(i).Cells(28).ForeColor = Drawing.Color.Red
                                    Else
                                        gridItem.Rows(i).Cells(28).ForeColor = Drawing.Color.Black
                                    End If
                                End If

                            End If
                        End If
                    End If
                End If
            End If
        Next
    End Sub

    Private Function GetNameMonth(ByVal Month As String) As String
        Try
            Select Case (CInt(Month).ToString("00"))
                Case "01"
                    Return "Jan"
                Case "02"
                    Return "Feb"
                Case "03"
                    Return "Mar"
                Case "04"
                    Return "Apr"
                Case "05"
                    Return "May"
                Case "06"
                    Return "Jun"
                Case "07"
                    Return "Jul"
                Case "08"
                    Return "Aug"
                Case "09"
                    Return "Sep"
                Case "10"
                    Return "Oct"
                Case "11"
                    Return "Nov"
                Case "12"
                    Return "Dec"
                Case Else
                    Return ""
            End Select
        Catch ex As Exception
            Return Month
        End Try

    End Function

    Private Sub btnReLoad_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReLoad.Click
        gridItem.DataSource = ViewState("ShoreProduct")
        gridItem.DataBind()
        LoadBOoking()
        ViewState("Load") = "Not"
        Timer1_Tick(sender, e)
    End Sub

    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "M")
        End If
    End Sub

    Private Sub btnSearchVPS_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearchVPS.Click
        LoadBOoking()
        ViewState("Load") = "Not"
        Timer1_Tick(sender, e)
    End Sub

End Class